# 
# Description :     Automotion programm result visualization      
# Author :          Golovchenko Aleksey
# Data :            2013/03/20
#

RUN_PWD=                            
RESULT_FOLDER=                      # directory contains result
RESULT_FILE_LST=                    # result files list
RESULT_FILE_CNT=                    # amount of files
 

# ---------------------------------------------------------------------------------
# Function definition
# ---------------------------------------------------------------------------------

#
# generate file header with plot settings for gnuplot script
#
GNUPLOT_SCRIPT=plot.graph
function gnuplot_template_header {   
    OUTPUT_FILE="result.png"
 
    GP_XRANGE_MIN=$1
    GP_XRANGE_MAX=$2

    echo "#! /usr/bin/gnuplot -persist" > $GNUPLOT_SCRIPT
    echo "set terminal png" >> $GNUPLOT_SCRIPT    
    echo "set output \"$OUTPUT_FILE\"" >> $GNUPLOT_SCRIPT
    echo "set encoding utf8" >> $GNUPLOT_SCRIPT
    echo "set xlabel \"Channel bit error probability\" font \"Helvetica,11\"" >> $GNUPLOT_SCRIPT
    echo "set ylabel \"Decoder output error porbability\" font \"Helvetica,11\"" >> $GNUPLOT_SCRIPT 
    echo "set logscale y" >> $GNUPLOT_SCRIPT
    echo "set key bottom right" >> $GNUPLOT_SCRIPT

    # line stiles for each result file
    i=1
    while true; do       
        echo "set style line $i lt $i pt 0" >> $GNUPLOT_SCRIPT
        if [ $i -eq $RESULT_FILE_CNT ]; then
            break;
        fi
        let "i+=1"
    done
    
    echo "set xrange [ $GP_XRANGE_MIN : $GP_XRANGE_MAX ]" >> $GNUPLOT_SCRIPT
    echo "set format y \"%.0e\"" >> $GNUPLOT_SCRIPT 
    echo "set format x \"%.2e\"" >> $GNUPLOT_SCRIPT 
    echo "set noxtics" >> $GNUPLOT_SCRIPT
    echo "set xtics $GP_XRANGE_MAX / 5, $GP_XRANGE_MAX / 5, $GP_XRANGE_MAX" >> $GNUPLOT_SCRIPT
    echo "set xtics add ($GP_XRANGE_MIN)" >> $GNUPLOT_SCRIPT
    echo "set grid xtics ytics ls 0" >> $GNUPLOT_SCRIPT

    echo "plot \\" >> $GNUPLOT_SCRIPT
}

#
# add data for gnuplot script
#
LINE_CNTR=0
function gnuplot_add_data {
    let "LINE_CNTR += 1"
    RES_FILE=$1
    N=$2
    K=$3
    t=$4

    TITLE=
    if [ $t -eq "0" ]; then 
        TITLE="Plain data("$N")"
    else
        TITLE="RS("$N","$K","$t")"
    fi
    
    if [ $LINE_CNTR -ne $RESULT_FILE_CNT ]; then
        echo "    \"$RES_FILE\" usi 1:2 title \"$TITLE\" with linespoints linestyle $LINE_CNTR, \\" >> $GNUPLOT_SCRIPT
    else
        echo "    \"$RES_FILE\" usi 1:2 title \"$TITLE\" with linespoints linestyle $LINE_CNTR" >> $GNUPLOT_SCRIPT
    fi
}

# ---------------------------------------------------------------------------------

# Check amount of arguments
if [ $# -lt 1 ]; then
    echo "Usage : "$0" path_to_folder_with_results"
    exit -1
fi

# Argument must be a durectory
RESULT_FOLDER=$1
if [ ! -d "$RESULT_FOLDER" ]; then
    echo "*** ERROR *** : "$RESULT_FOLDER" is not directory path!"
    exit -1
fi 

# check that the directory contains required files
RESULT_FILE_LST=$(ls -1 $RESULT_FOLDER | grep ^res_.*\.txt$);
RESULT_FILE_CNT=$(ls -1 $RESULT_FOLDER | grep ^res_.*\.txt$ | wc -l);
if [ -z "$RESULT_FILE_LST" ]; then
    echo "*** ERROR *** : "$RESULT_FOLDER" dose not contains result files!"
    exit -1
fi

# change current directory to result directory
RUN_PWD=$(pwd)
cd $RESULT_FOLDER

# proccess all result files
FIRST_FLAG=
for RES_FILE in $RESULT_FILE_LST; do

    # MAX ad MIN xrange value (first file)
    if [ -z "$FIRST_FLAG" ]; then        
        GP_XRANGE_MIN=$(cat $RES_FILE | awk {'print $1'} | head -1)    
        GP_XRANGE_MAX=$(cat $RES_FILE | awk {'print $1'} | tail -1)            

        # generate header for gnuplot script
        gnuplot_template_header $GP_XRANGE_MIN $GP_XRANGE_MAX

        FIRST_FLAG="1"
    fi

    N=$(echo $RES_FILE | awk -F"_" '{print $2}')
    K=$(echo $RES_FILE | awk -F"_" '{print $3}')
    t=$(echo $RES_FILE | awk -F"_" '{print $4}' | awk -F"." '{print $1}')

    # add data to gnuplot script
    gnuplot_add_data $RES_FILE $N $K $t    

done

# run gnuplot
echo "" >> $GNUPLOT_SCRIPT
cat $GNUPLOT_SCRIPT | gnuplot

# remove script
rm -f $GNUPLOT_SCRIPT

# back to run directory
cd $RUN_PWD
exit 0

